package com.dean.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class DeanNettyClient {
    // EventLoopGroup 事件循环组 用户监听及建立连接
    private final EventLoopGroup eventLoopGroup  = new NioEventLoopGroup();
    // 服务端启动初始化
    private final Bootstrap bootstrap = new Bootstrap();
    // 1、将serverGroup和clientGroup注册到服务端的Channel上；
    // 2、注册一个服务端的初始化器MyNettyServerInitializer；
    // 3、该初始化器中的initChannel()方法会在连接被注册到Channel后立刻执行；
    // 5、最后将端口号绑定到8080；
    public void start() throws InterruptedException {
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new DeanNettyClientInit());
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1",8080).sync();
                channelFuture.channel().closeFuture().sync();
        eventLoopGroup.shutdownGracefully();
    }

    public static void main(String[] args) {
        DeanNettyClient client = new DeanNettyClient();
        try {
            client.start();
        } catch (InterruptedException e) {
            System.out.println("服务端连接异常:");
            e.printStackTrace();
        }
    }

}
